package com.fingard.dsp.bank.directbank.cmb01;

import com.fingard.dsp.bank.directbank.bankInterface.IInCustodyInfo;
import com.fingard.dsp.bank.reqrespobj.qryincustodyinfo.ReqQryInCustodyInfo;
import com.fingard.dsp.bank.reqrespobj.qryincustodyinfo.RetAtsInCustodyInfo;
import com.fingard.dsp.bank.reqrespobj.qryincustodyinfo.RetAtsInCustodyInfoDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/9/21.
 */
public class CMB01QryInCustodyInfo extends CMB01Base implements IInCustodyInfo {
    @Override
    public RetAtsInCustodyInfo qryInCustodyInfo(ReqQryInCustodyInfo recInfo) throws Exception {
        RetAtsInCustodyInfo retAts = new RetAtsInCustodyInfo(reqHeader);
        retAts.writeStart();
        String tmpRespCode ="";
        String tmpRespInfo ="";
        try {
            CMB01Header header =  new CMB01Header();
            header.FUNNAM = "TDAFCIQY";
            XmlTextWriter xmlWriter =  new XmlTextWriter();
            writeCommonHeader(xmlWriter,header);
//            输入	TDAFCIQYX	输入属性	单记录
            xmlWriter.writeStartElement("TDAFCIQYX");
//            直联企业系统客户号	COREID	C (10)	否
            xmlWriter.writeElementString("COREID",bankFront.org );
//            经销商代码	DLENUM	C (40)	否
            xmlWriter.writeElementString("DLENUM",recInfo.DealerCode );
//            车辆状态	CARSTU	C(2)	可	01-在途 02-在库 03-出库 04-已置换 05-已退车
            xmlWriter.writeElementString("CARSTU", "02");

            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                if (tmpBkReader != null) {
                    String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                    if("0".equals(bankRetCode)) {
                        tmpRespCode ="0";
                        tmpRespInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                        while (tmpBkReader.read("/CMBSDKPGK/TDAFCIQYY")){
//                            车辆状态	CARSTU	C(2)	可	01-在途 02-在库 03-出库 04-已置换 05-已退车
//                            String carStu = tmpBkReader.readedXElement.getSingleText("CARSTU");
//                            if(!"02".equals(carStu)&&!"03".equals(carStu)){
//                                continue;
//                            }
                            RetAtsInCustodyInfoDetail detail = new RetAtsInCustodyInfoDetail();
//                            经销商代码	DLENUM	C (40)	可
                            detail.DealerCode = tmpBkReader.readedXElement.getSingleText("DLENUM");
//                            底盘号/车架号	CHANUM	C(40)	否
                            detail.VIN = tmpBkReader.readedXElement.getSingleText("CHANUM");
//                            合同单价	CRRAMT	M	否	合同单价
                            detail.PickupPrice = tmpBkReader.readedXElement.getSingleText("CRRAMT");
//                            到期日	ENDDAT	D	可	取最先到期日
                            detail.CarEneDate = tmpBkReader.readedXElement.getSingleText("ENDDAT");
//                            票号	BILLNO	C(100)	可	多张票号按逗号分隔
                            detail.BillNo = tmpBkReader.readedXElement.getSingleText("BILLNO");
//                            库存地点类型	STOTYP	C(1)	可	1: 主库3：二网
//                            库存地点	STOARA	Z(100)	可
                            String addrType = tmpBkReader.readedXElement.getSingleText("STOTYP");
                            if(StringHelper.hasAnyChar(addrType)){
                                if("1".equals(addrType)){
                                    detail.MainRepositoryLocation  = tmpBkReader.readedXElement.getSingleText("STOARA");
                                }else if("3".equals(addrType)){
                                    detail.SecondRepositoryLocation  = tmpBkReader.readedXElement.getSingleText("STOARA");
                                }
                            }
                            retAts.writeRetDetail(detail);
                        }
                    }else{
                        String bankRetMsg =tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                        throw new Exception(bankRetCode+":"+bankRetMsg);
                    }
                }else {
                    throw new Exception(tmpBkReader.toString());
                }
            } else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            tmpRespCode ="-1";
            tmpRespInfo = ex.getMessage();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
